package thread;

public class DaemonThreadDemo {
    public static void main(String[] args) {

        Thread rose = new Thread() {
            public void run() {
                for (int i = 0; i < 5; ++i) {
                    System.out.println("rose:Do you want to follow me?");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("rose:I am going to sleep");
                System.out.println("睡眠");
            }
        };

        Thread jack = new Thread() {
            public void run() {
                while (true) {
                    System.out.println("jack:you jump! i jump!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };

        rose.start();
        //设置为守护线程,守护线程不针对单一用户线程
        jack.setDaemon(true);
        jack.start();

        System.out.println("main方法执行完毕！");
        //while (true) ;如果主线程不结束，进程中因为还有用户线程活着，就不会结束
        //此时就不会杀死守护线程 ps：主线程不是守护线程
    }
}
